# -*- py-indent-offset: 4 -*- 
# -*- tab-width: 8 -*- 
# -*- indent-tabs-mode: nil -*-
from PrettyPrinter import Print
from etgui import *
import CmdNo,string
from PageLayout import *
from ModelDictEditor import AbstrModelDictEditor,ModelDictEditor
from ModelDictEditor import SimpleModelDictEditor

class TestModelDictEditor(SimpleModelDictEditor):
	def __init__(self,model,path,what):
		SimpleModelDictEditor.__init__(self,model,path,what)
	def DoEdit(self,i):
		print self.model.GetIndex(self.path)
		Print(self.model.GetValue(self.GetPathAt(i)))
	def NewValue(self):
		return {
			"__frame__":"File_Types_Info",
			"Name" : "NewItem"
		}
	def MakeName(self,name,i):
		return "%s(%i)" %(name,i)

class FileTypeCVEditor(ModelDictEditor):
	preview1=None
	def NewValue(self):
		return {
			"__frame__":"File_Types_Info",
			"Name" : "NewItem"
		}
 
	def Init(self):
		if not self.preview1:
			self.preview1=PotTextField("?")
			self.preview2=PotTextField("?")
			self.preview3=PotGuiDirSelector("?",gTextFieldLen)
 
	def GetPreview(self):
		self.Init()
		return MakeBorderedSectionLayout("General",
			Form(
				items=[
					"Signatures",self.preview1,
					"Type",self.preview2,
					"Directory",self.preview3
				]
			)
		)
	def SelectionChanged(self):
		if self.preview1:
			self.preview1.SetSlot(self.Slot("Signatures"))
			self.preview2.SetSlot(self.Slot("Type"))
			self.preview3.SetSlot(self.Slot("Directory"))
	def MakeItemEditView(self):
		derivedFrom=PotGuiPopupButton(self.Slot("Derived From"))
		derivedFrom.AddItem("File Type","")   # XXX add dynamically
 
		return MakePageLayout(eVObjHExpand|eVObjVExpand,
			MakeBorderedSectionLayout("SNiFF+ Build System",
				Form(
					items=[
						"General Makefile",PotGuiFileSelector(self.Slot("General Makefile"),gTextFieldLen,"File"),  
						"Generated From",MakeFieldLayout(derivedFrom)
					]
				)
			),
			MakeBorderedSectionLayout("Project Management and File Sharing",
				PotGuiToggleButton(self.Slot("Add/Remove Automatically"),"Add/Remove automatically to/from Project"),
				PotGuiToggleButton(self.Slot("Create Symbolic Link"),"Share with symbolic Links between WE\'s")
			),
			MakeBorderedSectionLayout("Advanced Settings",
				Form(
					items=[
						"SNiFF+ Source Code Parser",PotGuiFileSelector(self.Slot("Parser"),gTextFieldLen,"Parser"),  
						"Icon for Project File View",PotGuiFileSelector(self.Slot("Icon"),gTextFieldLen,"Icon"),
						"Retriever Priority",PotTextField(self.Slot("Retriever Priority"))
					]
				),
				PotGuiToggleButton(self.Slot("Is Default File Type"),"Is Default File Type")
			)
		)

class ModelSwitcher:
	def __init__(self,decorator,models):
		self.decorator=decorator
		self.models=models
		self.i=0
	def Next(self):
		i=self.i=(self.i+1)%len(self.models)
		self.decorator.SetModel(self.models[i])
class GotoModel:
	def __init__(self,decorator,model):
		self.decorator=decorator
		self.model=model
	def __call__(self):
		self.decorator.SetModel(self.model)
class Observer:
	def __init__(self,name):
		self.name=name
	def __call__(self,*args):
		print self.name,args
def MakeDialog(models):
	i=0
	model=PotGuiModelDecorator(models[0])
	model.AddModelObserver(Observer(`i`))
	for m in models:
		i=i+1
		m.AddModelObserver(Observer(`i`))
	modelswitcher=ModelSwitcher(model,models)
	modelio=ModelIO(model)
 
	view=MakeModelDictEditorLayout(FileTypeCVEditor(model,"FILE_TYPES","FileType"))
	panel=MakeTabSheetLayout(
		PseudoTabPage(
			"User",
			view,
			action=GotoModel(model,models[0])
		),
		PseudoTabPage(
			"Site",
			view,
			action=GotoModel(model,models[1])
		),
	)
	dialog=PotGuiDialog(
		model,
		MakeDialogWithButtons(
			panel,
			ActionButton(CmdNo.cIdOk,"OK",default=1),
			ActionButton(CmdNo.cIdCancel,"Cancel"),
			ActionButton("Load",action=modelio.Load),
			ActionButton("Save",action=modelio.Save),
			ActionButton("Next",action=modelswitcher.Next),
 
		),
		"Pot GUI",
		eWinDefault
	)
	return dialog
gDialog=None
def Show():
	global gDialog
	if not gDialog:
		models=[]
		model=PotGuiModel()
		model.SetValue("FILE_TYPES",EnvGetPot("FILE_TYPES"))
		models.append(model)
 
		model=PotGuiModel()
		model.SetValue("FILE_TYPES",EnvGetDict("FILE_TYPES"))
		models.append(model)
 
		gDialog=MakeDialog(models)
	gDialog.ShowUnderMouse()
